using System;
using System.Collections.Generic;
using System.Text;
using FlatRedBall;
using FlatRedBall.Input;
using FlatRedBall.AI.Pathfinding;
using FlatRedBall.Graphics.Animation;
using FlatRedBall.Graphics.Particle;

using FlatRedBall.Math.Geometry;
using FlatRedBall.Math.Splines;
using BitmapFont = FlatRedBall.Graphics.BitmapFont;
using Cursor = FlatRedBall.Gui.Cursor;
using GuiManager = FlatRedBall.Gui.GuiManager;

#if FRB_XNA || SILVERLIGHT
using Keys = Microsoft.Xna.Framework.Input.Keys;
using Vector3 = Microsoft.Xna.Framework.Vector3;
using Texture2D = Microsoft.Xna.Framework.Graphics.Texture2D;


#endif
using NereusGame.HUD;
using NereusGame.Entities.Weapons;
namespace NereusGame.Entities.Ships.Enemies
{
	public partial class EnemyBoss
	{
        private bool Visible;

        private enum BossState
        {
            Phase1, Phase2
        }

        private int initialPattern1Cycle2;
        private int initialPattern2Cycle2;
        private int initialPattern1Cycle;
        private int initialPattern2Cycle;
        private int pattern1Cycle;
        private int pattern2Cycle;
        private BossState mBoststate;
        private const int MAX_WEAPONS = 2;
        public const float MAX_HEALTH = 500f;      // Initial Health of the player.
        private const float BODY_RADIUS = 3.6f;
        private BossHud hud;

        private void CustomInitialize()
        {
            hud = new BossHud(SpriteManager.Camera, this);
            InitializeBoss();
            InitializeWeApons();

        }

        private void CustomActivity()
        {
            if (CurrentHealth <= MAX_HEALTH * 0.5f)
                mBoststate = BossState.Phase2;

            if (Visible)
            {
                hud.Show();
                hud.Update();
            }
            else
                hud.Hide();


        }

        private void CustomDestroy()
        {
            hud.Destroy();
            base.Destroy();

        }

        private static void CustomLoadStaticContent(string contentManagerName)
        {


        }


        private void InitializeBoss()
        {
            IsBoss = true;
            Tex.Texture = bossTexture;
            Tex.ScaleX = Tex.ScaleY = Body.Radius = BODY_RADIUS;
            CurrentHealth = MaxHealth = MAX_HEALTH;
            HealthBar.RelativePosition = new Vector3(0, Body.Radius + HEALTHBAR_OFFSET, 0);
            PrimaryTargetRange = 60f;
            IsNeutralMiner = false;
            Position = new Vector3(2000, 4000, 0);
            Visible = false;
            mBoststate = BossState.Phase1;
            initialPattern1Cycle2 = 250;
            initialPattern1Cycle = pattern1Cycle = 400;
            initialPattern2Cycle2 = 80;
            initialPattern2Cycle = pattern2Cycle = 50;
            points = 2000;

        }

        private void InitializeWeApons()
        {
            weaponInventory = new Weapon[MAX_WEAPONS];
            weaponInventory[0] = new Gun(CONTEN_MANAGER);
            weaponInventory[0].UpgradeWeapon();    // set gun's level to 1
            weaponInventory[0].UpgradeWeapon();    // set gun's level to 2
            weaponInventory[0].UpgradeWeapon();    // set gun's level to 2
            weaponInventory[0].SetFireRate(32);

            weaponInventory[1] = new Gun(CONTEN_MANAGER);
            weaponInventory[1].UpgradeWeapon();    // set gun's level to 1
            weaponInventory[1].UpgradeWeapon();    // set gun's level to 2
            weaponInventory[1].UpgradeWeapon();    // set gun's level to 2
            weaponInventory[1].UpgradeWeapon();    // set gun's level to 2
            weaponInventory[1].SetFireRate(25);
            weaponSelector = 0;
        }


        protected override void NormalState()
        {
            Visible = false;
        }

        protected override void AngryState()
        {
            Visible = true;
            Move2Player();
            if (mBoststate == BossState.Phase1)
                UpdatePhase1();
            else
                UpdatePhase2();
        }

        private void UpdatePhase1()
        {
            pattern1Cycle--;
            ShootPlayer();
            if (pattern1Cycle < 0)
            {
                weaponSelector = 1;
                pattern2Cycle--;

                if (pattern2Cycle < 0)
                {
                    weaponSelector = 0;
                    pattern1Cycle = initialPattern1Cycle;
                    pattern2Cycle = initialPattern2Cycle;
                }
            }
            

        }

        private void UpdatePhase2()
        {
            pattern1Cycle--;
            ShootPlayer();
            if (pattern1Cycle < 0)
            {
                
                weaponSelector = 1;
                pattern2Cycle--;

                if (pattern2Cycle < 0)
                {
                    Velocity = Vector3.Zero;
                    Vector3 direction = (PrimaryTarget.Position - Position);
                    direction.Normalize();
                    Position = PrimaryTarget.Position + direction * 20f;
                    weaponSelector = 0;
                    pattern1Cycle = initialPattern1Cycle2;
                    pattern2Cycle = initialPattern2Cycle2;
                }
            }

        }
        
        private void ShootPlayer()
        {
            Vector3 directionToPtimaryTarget = PrimaryTarget.Position - Position;
            directionToPtimaryTarget.Normalize();

            if (directionToPtimaryTarget.X >= 0)
                RotationZ = -(float)Math.Acos(directionToPtimaryTarget.Y);
            else
                RotationZ = (float)Math.Acos(directionToPtimaryTarget.Y);

            weaponInventory[weaponSelector].Shoot(this, Position, (BODY_RADIUS + 1f), RotationZ, Velocity);
        }


        private void Move2Player()
        {
            if ((Position - PrimaryTarget.Position).Length() > PrimaryTargetRange)
            {
                Drag = FRICTION;
                Vector3 directionToPrimaryTargetyTarget = PrimaryTarget.Position - Position;
                directionToPrimaryTargetyTarget.Normalize();
                Acceleration = directionToPrimaryTargetyTarget * 25f;
            }
            else
            {
                Acceleration = Vector3.Zero ;
                Drag = FRICTION * 4f;
            }
        }


       
    }
}
